<?php
	App::import('Vendor','Searchable.GigaSearch');
	App::import('Helper','Cms.Form');
	class SearchFormHelper extends FormHelper
	{
		protected $GigaSearchObjects;
		protected $rangeFieldSelectType = "select";
		var $defaultEmpty = "All";
		protected function getSearchFieldType($field)
		{
			$searchType = $this->getGigaSearchObject()->getSearchFieldType($field);
			return $searchType;
		}
		function create($model=null,$options = array())
		{
            $this->defaultEmpty = __("All");
			$this->Javascript->link('/searchable/js/giga_search.js',false);
			return parent::create($model,$options);
		}
		
		function input($field,$options = array())
		{
			$out = "";
			$model = ClassRegistry::getObject($this->model());
			$passedOptions = $options;
			$searchType = $this->getSearchFieldType($field);
			$searchConfig = $this->getSearchFieldConfig($field);
			$options = $model->getFieldProperties($field);
			$options['search_type'] = $searchType;
			$options = am($options,$passedOptions);
			$options = am($options,$searchConfig);
			$searchType = $options['search_type'];
			if(!isset($options['div']['class']))
			{
				$options['div']['class'] = "";
			}
			$options['div']['class'] = $searchType."_searchable"; 
			$searchTypeMethod = $this->searchTypeMethod($searchType);
			//@dirty hack.
			$this->rangeFieldSelectType = "select";
			if(method_exists($this,$searchTypeMethod))
			{
				$out = $this->$searchTypeMethod($field,$options);
			}
			return $out;
		}
		
		protected function getSearchFieldConfig($field)
		{
			$model = ClassRegistry ::getObject($this->model());
			$properties = $model->getFieldProperties($field,'searchOptions');
			if(!$properties)
			{
				$properties = array();
			}
			return $properties;
		}
		function search_select($field,$options)
		{
			//do not we should do this.But limitation of field contury suggestor made me do this.
			if(isset($options['search_type']) && (!isset($options['type']) || !$options['type']))
			{
				$options['type'] =$options['search_type'];
		
			}
			$defaultEmpty = __($this->defaultEmpty);
			if(!isset($options['empty']) && $defaultEmpty)
			{
				$options['empty'] = __($defaultEmpty);
			}
			return parent::input($field,$options);
		}
	
		private function searchTypeMethod($type)
		{
			return 'search_'.$type;
		}
		
		function search_radio($field,$options = array())
		{
			$options['type'] = "radio";
			return parent::input($field,$options);
		}
		
		function search_text($field,$options = array())
		{
			$this->rangeFieldSelectType  = "radio";
			return $this->rangeField($field,$options,array('selectOptions'=>array('default'=>GigaSearch::OP_LIKE)));
		}
		
		function search_number($field,$options = array())
		{
			return $this->rangeField($field,$options);
		}
		function search_date($field,$options = array())
		{
			return $this->rangeField($field,$options,array('type'=>"datePicker",'date_format'=>"yy-mm-dd"));
		}
		
		function search_checkbox($field,$options)
		{
			$options['type'] = "checkbox";
			return parent::input($field,$options);
		}
		protected function rangeField($field,$options = array(),$rangeOptions = array())
		{
			$matches = $this->getGigaSearchObject()->getAllowedRangeTypesAsOptions($field);
			$searchType = $this->getGigaSearchObject()->getSearchFieldType($field);
			$defaults = array('match'=>true);
			$options = am($defaults,$options);
			$options['matchOptions'] = $this->getGigaSearchObject()->getAllowedRangeTypesAsOptions($field);
			$configMatchOptions = cr('Searchable.'.$searchType.'.matchOptions');
			if($configMatchOptions)
			{
				$options['matchOptions'] = $configMatchOptions;
			}
			if($options['match'] == true)
			{
				//@TODO opreator is temporary
				$out = $this->getRangeInput($field,$options['matchOptions'],$rangeOptions);
				$labelText = null;
				if(isset($options['label']))
				{
					$labelText = __($options['label']) ;
				}
				$label = $this->label($field,$labelText,array() );
				$out = $label.$out;
				$divAttr = am($options['div'],array('class'=>"input text"));
				$divAttr['class'] .= ' '.$options['div']['class']; 
				$out = $this->Html->tag('div',$out,$divAttr);  
			}else
			{
				$options['type'] = "text"; 
				$out = parent::input($field,$options);
			}
			return $out;
		}
		protected function getRangeInput($field,$options,$rangeOptions = array())
		{
				$opreatorsOutput = "";
				$minRangeOutput = "";
				$maxRangeOutput = "";
				$betweenRange = false;
				$textField = $this->model().'.'.$field.'.value_min';
				$defaultTextOptions = array('type'=>"text","div"=>false,'label'=>false,'class'=>"range_input");
				if(isset($rangeOptions['selectOptions']))
				{
					$selectOptions = $rangeOptions['selectOptions'];
					unset( $rangeOptions['selectOptions']);
				}
				$textOptions = am($defaultTextOptions,$rangeOptions);
                $minFieldId = $this->domId($textField);  
				$minRangeOutput = parent::input($textField,$textOptions);
				if(isset($options[GigaSearch::OP_BETWEEN]))
				{
					$betweenRange = true; 
					$textField = $this->model().'.'.$field.'.value_max';
					$textOptions['id'] = $this->domId($textField);
					$maxFieldId = $textOptions['id'];
					$maxRangeOutput = parent::input($textField,am($textOptions,array("style"=>"display:none")));
				}
				
				$selectField = $this->model().'.'.$field.'.opreator';
				$selectOptions['type'] = $this->rangeFieldSelectType;
				$selectOptions['class']= "range_options_".$this->rangeFieldSelectType;
				$selectOptions['label'] = false;
				$selectOptions['div'] = false;
				if($betweenRange)
				{
					$selectOptions['onchange'] = "GigaSearch.toggleBetween(this,'$maxFieldId','$minFieldId')";
                    $this->Javascript->startup("GigaSearch.checkToggleOnLoad('$maxFieldId','$minFieldId');");
				}
				$selectOptions['options'] = $options;
				$opreatorsOutput = parent::input($selectField,$selectOptions);
				$out = "";
				$span = '<span></span>';
				$out = $opreatorsOutput.$span.$minRangeOutput.$span.$maxRangeOutput;
				return $out;
		}
		
		protected function getGigaSearchObject()
		{
			$modelAlias = $this->model();
			if(isset($this->GigaSearchObjects[$modelAlias ]) == false)
			{
				$this->GigaSearchObjects[$modelAlias ] = new GigaSearch(ClassRegistry::getObject($modelAlias) );
			}
			return $this->GigaSearchObjects[$modelAlias ];
		}
	}
?>